package com.lagou.myblog.controller;

import com.lagou.myblog.domain.EasyUIBean;
import com.lagou.myblog.domain.LqResult;
import com.lagou.myblog.entity.TArticle;
import com.lagou.myblog.service.TArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class TArticleController {
    @Autowired
    private TArticleService articleService;

    /**
     * 访问方式：http://localhost:7013/article/one.json?id=1
     */
    @RequestMapping("/one.json")
    public LqResult findOne(Integer id) {
        return LqResult.build(articleService.findOne(id));
    }

    /**
     * 访问方式：http://localhost:7013/article/query.json
     */
    @RequestMapping("/query.json")
    public EasyUIBean query(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "rows", required = false, defaultValue = "10") Integer rows
    ) {
        return articleService.queryAll(page, rows);
    }

    @RequestMapping(value = "/save.json", method = {RequestMethod.POST})
    public LqResult saveBean(@RequestBody TArticle bean) {
        return LqResult.build(articleService.saveBean(bean));
    }

    @RequestMapping(value = "/update.json", method = {RequestMethod.PUT})
    public LqResult updateBean(@RequestBody TArticle bean) {
        TArticle tArticle = articleService.updateBean(bean);
        return LqResult.build("修改成功", tArticle);
    }

    @RequestMapping(value = "/delete.json", method = {RequestMethod.DELETE})
    public LqResult deleteBean(Integer id) {
        articleService.deleteBean(id);
        return LqResult.build("删除成功");
    }

}
